package sz.szsmk.citizencard.mvp.dzsmkpresenter;

import com.google.gson.Gson;

import javax.inject.Inject;

import rx.functions.Action1;
import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.db.DaoSession;
import sz.szsmk.citizencard.db.SysConfDao;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.dzbean.req.CommonRequest;
import sz.szsmk.citizencard.model.network.dzbean.req.GetOpenIdReq;
import sz.szsmk.citizencard.model.network.dzbean.req.PreAuthRequest;
import sz.szsmk.citizencard.model.network.dzbean.req.PreAuthThiedReq;
import sz.szsmk.citizencard.model.network.dzbean.req.PreAuthThirdRes;
import sz.szsmk.citizencard.model.network.dzbean.res.GetOpenIdRes;
import sz.szsmk.citizencard.model.network.dzbean.res.PreAuthRes;
import sz.szsmk.citizencard.model.network.dzbean.res.PreAuthResult;
import sz.szsmk.citizencard.model.network.dzbean.res.SysConf;
import sz.szsmk.citizencard.model.network.dzbean.res.SysResponse;
import sz.szsmk.citizencard.mvp.MainActMvpView;
import sz.szsmk.citizencard.retrofitrxjava.DZSmkAppInit;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.HttpApi;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.smkretrofit.NetworkDateSource;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.ObjectSignatureUtil;
import sz.szsmk.citizencard.widget.GlobalVariables;

/**
 * Created by chensf on 17/10/1.
 */

public class MainActPresenter extends AppBasePresenter<MainActMvpView> {

    @Inject
    NetworkDateSource mNetworkDateSource;
//    private final NetworkDateSource mNetworkDateSource;

    private NetworkDateSources mnetworkDateSource;

    @Inject
    public MainActPresenter() {
        getComponent().inject(this);
//        mNetworkDateSource = NetworkDateSource.getInstance();

        this.mnetworkDateSource = NetworkDateSources.getInstance();
    }

    public void preAuthThird(String openId, String mobile, int userId) {
        PreAuthThiedReq preAuthThiedReq = new PreAuthThiedReq();
        preAuthThiedReq.setMobileNumber(mobile);
        preAuthThiedReq.setOpenId(openId);
        preAuthThiedReq.setUserId(userId + "");
        preAuthThiedReq.setPhotoUrl("asdf");
        ObjectSignatureUtil.signaturePreAuthRequest(preAuthThiedReq);
        String toJson = new Gson().toJson(preAuthThiedReq);
        L.e(Constant.TAG, "preAuthThird------>" + toJson);
        mNetworkDateSource.preAuthThird(preAuthThiedReq, new Action1<PreAuthThirdRes>() {
            @Override
            public void call(PreAuthThirdRes preAuthRes) {
                PreAuthThirdRes.DataBean data = preAuthRes.getData();
                String json = new Gson().toJson(data);
                ACache.get(DaggerApplication.getIns()).put("token", preAuthRes.getData().getToken());
                PreAuthResult preAuthResult = new Gson().fromJson(json, PreAuthResult.class);
                GlobalVariables.getInstance(DZSmkAppInit.getInstance().getContext()).setPreAuthResult(getActivity(), preAuthResult);
                getSysConfig();
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                L.e(Constant.TAG, "--------------------" + throwable.getMessage());
            }
        });

    }

    public void preAuth(String userId, String mobileNumber, String photoUrl) {
        checkViewAttached();
        PreAuthRequest preAuthRequest = new PreAuthRequest();
        //三个参数
//        preAuthRequest.setUserId(userId);
        preAuthRequest.setUserId("123456");
//        preAuthRequest.setMobileNumber(mobileNumber);
        preAuthRequest.setMobileNumber("15212340021");
        preAuthRequest.setPhotoUrl(photoUrl);
        ObjectSignatureUtil.signaturePreAuthRequest(preAuthRequest);

        HttpApi httpApi = mNetworkDateSource.getHttpApi();


        mNetworkDateSource.preAuth(preAuthRequest, new Action1<PreAuthRes>() {
            @Override
            public void call(PreAuthRes preAuthRes) {
                PreAuthRes.DataBean data = preAuthRes.getData();
                String json = new Gson().toJson(data);
                PreAuthResult preAuthResult = new Gson().fromJson(json, PreAuthResult.class);

                GlobalVariables.getInstance(DZSmkAppInit.getInstance().getContext()).setPreAuthResult(getActivity(), preAuthResult);
                getSysConfig();
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                String message = throwable.getMessage();
            }
        });
    }

    public void getOpenId() {
        GetOpenIdReq getOpenIdReq = new GetOpenIdReq();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        getOpenIdReq.setUser_type(0);
        getOpenIdReq.setAccess_token(access_token);
        mnetworkDateSource.getOpenId(getOpenIdReq, new SubscribeResult<GetOpenIdRes>() {
            @Override
            protected void onOk(GetOpenIdRes response) {
                String json = new Gson().toJson(response);
                L.e(Constant.TAG, "getOpenId---->" + json);
                String openId = response.getOpenId();
                ACache.get(DaggerApplication.getIns()).remove("openId");
                ACache.get(DaggerApplication.getIns()).put("openId", openId);
                getMvpView().getOpenIdSucc(response);
            }

            @Override
            protected void onFailure(GetOpenIdRes response) {
                super.onFailure(response);
                L.e(Constant.TAG, "getOpenId---->" + response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                L.e(Constant.TAG, "getOpenId---->" + throwable.getMessage());

            }
        });
    }

//    public void getPersonInfo() {
//        checkViewAttached();
//        HttpApi httpApi = mNetworkDateSource.getHttpApi();
//        CommonRequest commonRequest = ObjectSignatureUtil.getCommonRequest();
//
////
////        httpApi.getUserInfo(commonRequest).compose(RxNetworkHelp.<User>handleResult()).subscribe(new RxSubscribe<User>(getActivity()) {
////            @Override
////            protected void onOk(User response) {
////                getMvpView().onGainPersonInfo(response);
////            }
////        });
////
//        mNetworkDateSource.getUserInfo(commonRequest, new Action1<GetUserInfoRes>() {
//            @Override
//            public void call(GetUserInfoRes baseRes) {
//                String code = baseRes.getCode();
//                if (code.equals("0000")) {
//                    Object data = baseRes.getData();
//                    String json = new Gson().toJson(data);
//                    User user = new Gson().fromJson(json, User.class);
//                    String idCardNumber = user.getIdCardNumber();
//                    ACache.get(DaggerApplication.getIns()).put("userIdCard", idCardNumber);
//                    getMvpView().onGainPersonInfo(user);
//                }
//            }
//        }, new ErrorAction() {
//            @Override
//            public void onCall(Throwable throwable) {
//
//            }
//        });
//
//
//    }

    private void getSysConfig() {
        checkViewAttached();
        CommonRequest commonRequest = ObjectSignatureUtil.getCommonRequest();

        String s = new Gson().toJson(commonRequest);
        L.e(Constant.TAG, "sysConfig----->" + s);
        mNetworkDateSource.sysConfig(commonRequest, new Action1<SysResponse>() {
            @Override
            public void call(SysResponse sysResponse) {
                String code = sysResponse.getCode();
                if (code.equals("0000")) {
                    SysResponse.DataBean data = sysResponse.getData();
                    String json = new Gson().toJson(data);
                    SysConf sysConf = new Gson().fromJson(json, SysConf.class);
                    if (sysConf != null) {
                        DaoSession daoSession = DaggerApplication.getmDaoSession();
                        SysConfDao sysConfDao = daoSession.getSysConfDao();
                        sysConfDao.deleteAll();
                        sysConfDao.save(sysConf);
                        getMvpView().onPreAuthSuccess();
                    }
                } else {
                    getMvpView().onPreAuthFailure();
                }

            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }
}
